Skip to content

perf(vthread): cut the memory of a parked virtual thread from 3.8 KB to 2.6 KB - #97

Merged
Project516 merged 5 commits into
nextfrom
p516/vthread-mem
Nov 22, 2026
Merged

Project516 merged 5 commits into
nextfrom
p516/vthread-mem

Conversation

@Project516

Copy link
Copy Markdown
Owner

What this changes

A parked virtual thread now costs 2.6 KB of JS heap instead of 3.8 KB (32% less), measured with 10,000 parked threads and the memory MXBean after System.gc().

  • A yielded continuation's frames are packed into one array with a two-value header per frame (method, then pc and sp in one number), instead of one array per frame. A frame that holds a monitor stays a Frame.
  • All continuations share two StackChunk objects, one for yielded and one for mounted, since Continuation.isEmpty and isStarted only read them.
  • A monitor's entry and wait queues are allocated while a thread uses them, not for every inflated monitor.

Skynet (a million virtual threads under -Xmx1500m) still runs out of heap. The rest of a parked thread is Java objects HotSpot has too (the VirtualThread, its Continuation, runnable, lock and container node), about 1.3 to 1.6 KB, so fitting it needs a smaller object layout. Skynet100kWithMonitors rounds went from 16 to 22 s down to about 10 s, still over its timeout.

Tests

  • Bytes per parked thread, 10,000 threads:
Commit Depth 0 Depth 10 In wait
next 3,798 5,361 4,089
pack a continuation's frames into one array 2,903 3,755 3,421
share the two stack chunks 2,752 3,643 3,269
allocate monitor queues lazily 2,647 3,509 3,162
one packed header value, one flags field 2,563 3,270 3,046
  • Timing, next against branch alternating, 5 runs each, median: virtual park ping-pong 565 / 555 ms, virtual wait/notify ping-pong 585 / 584 ms, synchronized loop (20M) 291 / 284 ms, platform wait/notify (400,000 rounds) 636-660 / 625-659 ms.
  • New program VirtualPackedMix: 2,400 virtual threads parked in synchronized methods, a wait and plain frames, past the packing threshold; output matches HotSpot.
  • Full suite: 268 pass, 3 skipped. passing.txt tests under java/lang/Thread, java/util/concurrent and java/lang/management pass (the three tck/JSR166TestCase entries time out on next too).

@coderabbitai

coderabbitai Bot commented Nov 22, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 109f906d-06c0-4423-84bf-f3c6af1ef484

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@project516-review-bot project516-review-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR reduces parked virtual thread memory from 3.8 KB to 2.6 KB by packing yielded continuation frames into a single array, sharing two StackChunk objects across all continuations, and allocating monitor entry/wait queues lazily. The changes span the JVM monitor logic, continuation frame packing/unpacking, Object.wait/notify, and snapshot serialization. A new test VirtualPackedMix exercises the packing threshold with 2400 threads mixing synchronized, wait, and plain frames.


review-bot, model nvidia/nemotron-3-ultra-550b-a55b:free, verdict approve

@Project516
Project516 merged commit 1c579b5 into next Nov 22, 2026
12 checks passed
@Project516
Project516 deleted the p516/vthread-mem branch November 22, 2026 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant